<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>字典管理</title>
<link href="/res/third/bootstrap3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

<script src="/res/common/js/jquery.1.9.1.min.js" type="text/javascript"></script>
<script src="/res/third/artDialog/jquery.artDialog.js" type="text/javascript"></script>
<script src="/res/third/powerFloat/js/mini/jquery-powerFloat-min.js" type="text/javascript"></script>
<script src="/res/third/validate/jquery.validate.js" type="text/javascript"></script>

<script>
//当前节点的父
var parentId=${parentId!0};
//上一节点的父，用来返回父
var prevParent=${prevParent!0};
//弹窗
function showDialog(title){
	   dialog = art.dialog({
	        title: title,
	        width:500,
	        lock:true,
	        content: document.getElementById('hidden_dialog')
	    });
}
//新增
function add(){
	  document.formName.reset();
	  $("#parent_id").val(parentId);
	  $("#parentId").val(parentId);
	  showDialog("新增");
}
//提交
function sub(){
[#--提交之前 form 要手动进行验证--]
if($("#signupForm").valid()){
	art.dialog.post({
		url: "/diction/ajax_save.do",
        data:$("form").serialize(),
        success: function(data){
            if(data.success){
                location.reload();
            }else{
            	alert(data.msg)
            }
        }
	},"加载中...");
}
}
$().ready(function(){
	 //验证表单
    $("#signupForm").validate({
    	rules:{
    		name:{
    			required:true,
    			maxlength:10
    		},
    		code:{
    			required:true,
    			
    			maxlength:50
    		}
    	}
    });
});
//编辑
function edit(id,name,descr,value,sortnum){
	$("#id").val(id);
	$("#name").val(name);
	$("#descr").val(descr);
	$("#value").val(value);
	$("#sortnum").val(sortnum);
	showDialog("编辑");
}
//删除
function del(id){//弹出警告框判断是否删除
	if(confirm('是否确认删除')){
			$.ajax({
				url : '/diction/ajax_del.do',
				type : 'get',
				dataType : 'json',
				data : {
					id : id
				},
				beforeSend:function(){
		            art.dialog.tips("删除中...");
		        },
				success : function(data){
					if(data.success){
						location.reload();
					}else{
						error_msg(data.msg , function(){location.reload();});
					}
				}
			});
	}
}
//进入子级
function tochildren(id){
	window.location.href=getlocation()+"?nextP="+id+"&prevP="+parentId;
}
// 返回上一级
function backprev(){
	window.location.href=getlocation()+"?nextP="+prevParent;
}
//返回第一层
function backhome(){
	window.location.href=getlocation()+"?nextP=0";
}
function getlocation(){
	var l = window.location.href;
	var n = l.lastIndexOf('?');
	if(n<0){
		return l;
	}else{
		return l.substring(0,n);
	}
}

function changeSort(id , oldsort){
	var sort = $("#sortnum"+id).val();
	if(sort == ''){
		$("#sortnum"+id).val(oldsort);
		return false;
	}
	
	if(!new RegExp("\\d+").test(sort)){
		alert("输入非法");
		$("#sortnum"+id).val(oldsort);
	}
	$.ajax({
		url : '/diction/changeSort.do',
		type : 'post',
		dataType : 'json',
		beforeSend:function(){
			$("#sortnum"+id).parent().append('<img id="abc"  src="/res/common/images/loadding.gif" style="width: 20px;height: 20px;position:absolute;"  >');
        },
        complete:function(){
            $("#abc").remove();
        },
		data : {
			id : id ,
			sort : sort
		},
		success : function(data){
			if(data.res){
			}
		}
	});
}
</script>
</head>

<body>
	<div class="panel panel-info panel-content">
		<div class="panel-heading">
			[#if parentId?? && parentId != 0 ]
			<button onclick="backhome();" type="button" class="btn btn-success">
				<span class="glyphicon glyphicon-share-alt"></span>
			</button>
			<button onclick="backprev();" type="button" class="btn btn-success">
				<span class="glyphicon glyphicon-chevron-left"></span>
			</button>
			[/#if] [#assign canedit = false /] [@func_perm url="/diction/ajax_save.do" ] [#assign canedit = true /]
			<button type="button" onclick="add();" class="btn btn-info">
				<span class="glyphicon glyphicon-plus"></span>
				&nbsp;新增
			</button>
			[/@func_perm]
		</div>
		<table class="table table-bordered table-hover table-striped">
			<tr>
				<th width="50">#</th>
				<th>ID</th>
				<th>名称</th>
				<th>描述</th>
				<th>值</th>
				<th width="100">序号</th>
				<th width="200">操作</th>
			</tr>
			[#assign candel=false /] [@func_perm url="/diction/ajax_del.do" ] [#assign candel=true /] [/@func_perm] [#list dictions as dic]
			<tr>
				<td>${dic_index + 1}</td>
				<td>${dic.id!}</td>
				<td>${dic.name!}</td>
				<td>${dic.descr!}</td>
				<td>${dic.code!}</td>
				<td align="center">
					<input type="text" id="sortnum${dic.id!}" placeholder="请输入序号" name="sortnum" required="" value="${dic.sortnum!}" onblur="changeSort(${dic.id!} , ${dic.sortnum!});" style="width: 45px; text-align: center;" maxlength="4">
				</td>
				<td>
					[#if canedit ]
					<button onclick="edit(${dic.id!},'${dic.name!}','${dic.descr!}','${dic.code!}',${dic.sortnum!});" type="button" class="btn btn-primary btn-xs">编辑</button>
					[/#if] [#if candel ]
					<button onclick="del(${dic.id});" type="button" class="btn btn-danger btn-xs ">删除</button>
					[/#if]
					<button onclick="tochildren(${dic.id});" type="button" class="btn btn-success btn-xs">
						<span class="glyphicon glyphicon-chevron-right"></span>
					</button>
				</td>
			</tr>
			[/#list]
		</table>
	</div>

	<!-- 弹窗 -->
	<div id="hidden_dialog" style="display: none;">
		<form id="signupForm" class="form-horizontal" name="formName" role="form">
			<input type="hidden" name="parentId" id="parent_id" value="${parentId!0}" />
			<input type="hidden" name="id" id="id" />
			<div class="form-group">
				<label for="name">名称</label>
				<input class="form-control" id="name" name="name" />
			</div>
			<div class="form-group">
				<label for="name">描述</label>
				<input class="form-control" id="descr" name="descr" />
			</div>
			<div class="form-group">
				<label for="value">值</label>
				<input class="form-control" id="value" name="code" />
			</div>
			<div class="form-group">
				<label for="sortnum">序号</label>
				<input type="number" min="1" max="100" name="sortnum" class="form-control" id="sortnum" />
			</div>
			<div class="form-group">
				<div style="text-align: center;">
					<button onclick="sub();" type="button" class="btn btn-info">提交</button>
				</div>
			</div>
		</form>
	</div>
</body>
</html>